package com.framework.modules.base.service.impl;

import com.framework.common.utils.DateUtils;
import com.framework.modules.base.entity.UserDoingEntity;
import com.framework.modules.base.service.PostsService;
import com.framework.modules.base.service.UserDoingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.framework.common.utils.PageUtils;
import com.framework.common.utils.Query;

import com.framework.modules.base.dao.PostsCommentsDao;
import com.framework.modules.base.entity.PostsCommentsEntity;
import com.framework.modules.base.service.PostsCommentsService;


@Service("postsCommentsService")
public class PostsCommentsServiceImpl extends ServiceImpl<PostsCommentsDao, PostsCommentsEntity> implements PostsCommentsService {
    @Autowired
    private PostsService postsService;

    @Autowired
    private UserDoingService userDoingService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        Page<PostsCommentsEntity> page = this.selectPage(
                new Query<PostsCommentsEntity>(params).getPage(),
                new EntityWrapper<PostsCommentsEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public Integer updateCommentLikeCount(Integer id,Integer likeCount,String type) {
        if("add".equals(type)){
            return this.baseMapper.addCommentLikeCount(id);
        }else if("reduce".equals(type)){
            return this.baseMapper.reduceCommentLikeCount(id);
        }else{
            return this.baseMapper.updateCommentLikeCount(id,likeCount);
        }
    }

    @Override
    public Object commentItemAdd(PostsCommentsEntity entity) {
        entity.setAdddate(DateUtils.getSecondTimestamp(new Date()));
        this.baseMapper.insert(entity);
        return postsService.updatePostsCommentCount(entity.getPostId(),null,"add");
    }

    @Override
    public Object likeComment(UserDoingEntity entity) {
        Integer objectId=entity.getObjectId();
        boolean isExist=userDoingService.isExist(entity);
        //判断是否关注，喜欢过
        if(isExist){
            //如果存在，就删除喜欢
            userDoingService.deleteDoing(entity);
            //自加1
            this.baseMapper.reduceCommentLikeCount(objectId);
        }else{
            //如果不存在，就添加喜欢
            entity.setAdddate(DateUtils.getSecondTimestamp(new Date()));
            userDoingService.addDoing(entity);
            //自加1
            this.baseMapper.addCommentLikeCount(objectId);
        }
        return null;
    }

}
